using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace WcfService1.DTO
{
    [DataContract]
    public class SalesReturnDTO : INotifyPropertyChanged
    {
        private CostCenterDTO _CostCenter;
        private CustomerDTO _Customer;
        private String _Description;
        private int _No;
        private DateTime? _SRDate;
        private String _SRNo;
        private SalesInvoiceDTO _SalesInvoice;
        private ObservableCollection<SalesReturnDetailDTO> _SalesReturnDetails;
        private Decimal _Total;
        private String _created_by;
        private DateTime? _created_date;
        private String _modified_by;
        private DateTime? _modified_date;
        private WarehouseDTO _Warehouse;
        private Status _Status;

        public SalesReturnDTO()
        {
            SRDate = DateTime.Now.Date;
        }

        [DataMember]
        public String SRNo
        {
            get { return _SRNo; }
            set
            {
                _SRNo = value;
                RaisePropertyChanged("SRNo");
            }
        }


        [DataMember]
        public DateTime? SRDate
        {
            get { return _SRDate; }
            set
            {
                _SRDate = value;
                RaisePropertyChanged("SRDate");
            }
        }


        [DataMember]
        public SalesInvoiceDTO SalesInvoice
        {
            get { return _SalesInvoice; }
            set
            {
                _SalesInvoice = value;
                RaisePropertyChanged("SalesInvoice");
            }
        }


        [DataMember]
        public CustomerDTO Customer
        {
            get { return _Customer; }
            set
            {
                _Customer = value;
                RaisePropertyChanged("Customer");
            }
        }


        [DataMember]
        public String Description
        {
            get { return _Description; }
            set
            {
                _Description = value;
                RaisePropertyChanged("Description");
            }
        }


        [DataMember]
        public String created_by
        {
            get { return _created_by; }
            set
            {
                _created_by = value;
                RaisePropertyChanged("created_by");
            }
        }


        [DataMember]
        public DateTime? created_date
        {
            get { return _created_date; }
            set
            {
                _created_date = value;
                RaisePropertyChanged("created_date");
            }
        }

        [DataMember]
        public String modified_by
        {
            get { return _modified_by; }
            set
            {
                _modified_by = value;
                RaisePropertyChanged("modified_by");
            }
        }


        [DataMember]
        public DateTime? modified_date
        {
            get { return _modified_date; }
            set
            {
                _modified_date = value;
                RaisePropertyChanged("modified_date");
            }
        }

        [DataMember]
        public ObservableCollection<SalesReturnDetailDTO> SalesReturnDetails
        {
            get { return _SalesReturnDetails; }
            set
            {
                if (value == SalesReturnDetails) return;
                _SalesReturnDetails = value;
                RaisePropertyChanged("SalesReturnDetails");
                RefreshNumber();
            }
        }


        public int No
        {
            get { return _No; }
            set
            {
                _No = value;
                RaisePropertyChanged("No");
            }
        }

        public Decimal Total
        {
            get { return _Total; }
            set
            {
                _Total = value;
                RaisePropertyChanged("Total");
            }
        }

        [DataMember]
        public CostCenterDTO CostCenter
        {
            get { return _CostCenter; }
            set
            {
                _CostCenter = value;
                RaisePropertyChanged("CostCenter");
            }
        }

        [DataMember]
        public WarehouseDTO Warehouse
        {
            get { return _Warehouse; }
            set
            {
                _Warehouse = value;
                RaisePropertyChanged("Warehouse");
            }
        }

        [DataMember]
        public Status Status
        {
            get { return _Status; }

            set
            {
                _Status = value;
                RaisePropertyChanged("Status");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void RefreshNumber()
        {
            SalesReturnDetails.CollectionChanged += SalesReturnDetails_CollectionChanged;
            int number = 0;
            foreach (SalesReturnDetailDTO DetailDto in SalesReturnDetails)
            {
                number += 1;
                DetailDto.No = number;
            }
        }

        private void SalesReturnDetails_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            int number = 0;
            foreach (SalesReturnDetailDTO DetailDto in SalesReturnDetails)
            {
                number += 1;
                DetailDto.No = number;
            }
        }

        protected void RaisePropertyChanged(string propertyname)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyname));
        }
    }
}